<template>
  <div class="feedback-container">
    <div class="page-header">
      <h2>留言反馈</h2>
      <p>欢迎提出您的宝贵意见和建议，我们将认真考虑每一条反馈</p>
    </div>

    <!-- 留言表单 -->
    <div class="feedback-form-container">
      <el-form :model="feedbackForm" :rules="rules" ref="feedbackFormRef" label-width="100px">
        <el-form-item label="留言内容" prop="content">
          <el-input type="textarea" v-model="feedbackForm.content" :rows="4" placeholder="请输入您的意见、建议或问题..."></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="contactInfo">
          <el-input v-model="feedbackForm.contactInfo" placeholder="选填，方便我们与您联系（手机号/邮箱/微信等）"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitFeedback" :loading="submitting">提交留言</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 历史留言 -->
    <div class="feedback-history">
      <div class="history-header">
        <h3>我的留言历史</h3>
        <el-button type="primary" size="small" plain @click="fetchMyFeedbacks">刷新</el-button>
      </div>

      <el-empty v-if="myFeedbacks.length === 0" description="暂无留言记录"></el-empty>

      <div v-else class="feedback-list">
        <el-timeline>
          <el-timeline-item
            v-for="item in myFeedbacks"
            :key="item.id"
            :timestamp="item.createTime"
            :type="item.status === 1 ? 'success' : 'primary'"
          >
            <el-card class="feedback-card">
              <template #header>
                <div class="card-header">
                  <span>留言内容</span>
                  <el-tag v-if="item.status === 1" type="success">已回复</el-tag>
                  <el-tag v-else type="info">待处理</el-tag>
                </div>
              </template>
              <div class="feedback-content">{{ item.content }}</div>
              
              <!-- 管理员回复 -->
              <div v-if="item.status === 1" class="feedback-reply">
                <div class="reply-header">
                  <span class="reply-title">管理员回复</span>
                  <span class="reply-time">{{ item.replyTime }}</span>
                </div>
                <div class="reply-content">{{ item.reply }}</div>
                <div class="reply-footer">回复人: {{ item.replyUsername }}</div>
              </div>

              <!-- 删除按钮 -->
              <div class="feedback-actions">
                <el-popconfirm
                  title="确定要删除此条留言吗？"
                  @confirm="deleteFeedback(item.id)"
                >
                  <template #reference>
                    <el-button type="danger" size="small" text>删除</el-button>
                  </template>
                </el-popconfirm>
              </div>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { addFeedbackService, getMyFeedbacksService, deleteFeedbackService } from '@/api/feedback'

// 留言表单
const feedbackFormRef = ref(null)
const feedbackForm = ref({
  content: '',
  contactInfo: ''
})

// 表单验证规则
const rules = {
  content: [
    { required: true, message: '请输入留言内容', trigger: 'blur' },
    { min: 5, max: 500, message: '内容长度应在 5 到 500 个字符之间', trigger: 'blur' }
  ]
}

// 提交状态
const submitting = ref(false)

// 历史留言
const myFeedbacks = ref([])
const loading = ref(false)

// 提交留言
const submitFeedback = () => {
  feedbackFormRef.value.validate(async (valid) => {
    if (!valid) return
    
    try {
      submitting.value = true
      const res = await addFeedbackService(feedbackForm.value)
      
      if (res.code === 0) {
        ElMessage.success('留言提交成功')
        resetForm()
        // 刷新留言列表
        fetchMyFeedbacks()
      } else {
        ElMessage.error(res.msg || '提交失败')
      }
    } catch (error) {
      console.error('提交留言失败:', error)
      ElMessage.error('提交失败，请稍后再试')
    } finally {
      submitting.value = false
    }
  })
}

// 重置表单
const resetForm = () => {
  feedbackFormRef.value.resetFields()
}

// 获取我的留言列表
const fetchMyFeedbacks = async () => {
  try {
    loading.value = true
    const res = await getMyFeedbacksService()
    
    if (res.code === 0) {
      myFeedbacks.value = res.data
    } else {
      ElMessage.error(res.msg || '获取留言列表失败')
    }
  } catch (error) {
    console.error('获取留言列表失败:', error)
    ElMessage.error('获取留言列表失败，请稍后再试')
  } finally {
    loading.value = false
  }
}

// 删除留言
const deleteFeedback = async (id) => {
  try {
    const res = await deleteFeedbackService(id)
    
    if (res.code === 0) {
      ElMessage.success('删除成功')
      // 刷新留言列表
      fetchMyFeedbacks()
    } else {
      ElMessage.error(res.msg || '删除失败')
    }
  } catch (error) {
    console.error('删除留言失败:', error)
    ElMessage.error('删除失败，请稍后再试')
  }
}

// 组件挂载时获取留言列表
onMounted(() => {
  fetchMyFeedbacks()
})
</script>

<style scoped>
.feedback-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.page-header {
  text-align: center;
  margin-bottom: 30px;
}

.page-header h2 {
  font-size: 24px;
  color: #303133;
  margin-bottom: 10px;
}

.page-header p {
  color: #606266;
  font-size: 14px;
}

.feedback-form-container {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
}

.feedback-history {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.history-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.history-header h3 {
  margin: 0;
  font-size: 18px;
  color: #303133;
}

.feedback-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.feedback-content {
  margin-bottom: 15px;
  white-space: pre-wrap;
  line-height: 1.5;
}

.feedback-reply {
  background-color: #f5f7fa;
  padding: 15px;
  border-radius: 4px;
  margin-top: 15px;
}

.reply-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.reply-title {
  font-weight: bold;
  color: #409EFF;
}

.reply-time {
  font-size: 12px;
  color: #909399;
}

.reply-content {
  white-space: pre-wrap;
  line-height: 1.5;
  margin-bottom: 10px;
}

.reply-footer {
  text-align: right;
  font-size: 12px;
  color: #909399;
}

.feedback-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style> 